{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Importing modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pylab as pl\n",
    "from vtk_io import readVTK\n",
    "import athena_read"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### FARGO3D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# path to the data\n",
    "path_fargo = './fargo3d/'\n",
    "\n",
    "# this includes ghost zones\n",
    "phi_f   = np.fromfile(path_fargo+'domain_x.dat',sep='\\n')\n",
    "rad_f   = np.fromfile(path_fargo+'domain_y.dat',sep='\\n')\n",
    "theta_f = np.fromfile(path_fargo+'domain_z.dat',sep='\\n')\n",
    "\n",
    "# the following removes ghost zones\n",
    "phi_f   = 0.5 * (phi_f[:-1] + phi_f[1:])\n",
    "rad_f   = 0.5 * (rad_f[3:-4] + rad_f[4:-3])\n",
    "theta_f = 0.5 * (theta_f[3:-4] + theta_f[4:-3])\n",
    "\n",
    "# dimensions\n",
    "nx = len(phi_f)   # phi\n",
    "ny = len(rad_f)   # r\n",
    "nz = len(theta_f) # theta\n",
    "\n",
    "# reading in the data\n",
    "# initial conditions\n",
    "rho0_f    = pl.fromfile(path_fargo+'gasdens0.dat').reshape(nz,ny,nx).swapaxes(1,0)\n",
    "vphi0_f   = pl.fromfile(path_fargo+'gasvx0.dat').reshape(nz,ny,nx).swapaxes(1,0)\n",
    "vrad0_f   = pl.fromfile(path_fargo+'gasvy0.dat').reshape(nz,ny,nx).swapaxes(1,0)\n",
    "vtheta0_f = pl.fromfile(path_fargo+'gasvz0.dat').reshape(nz,ny,nx).swapaxes(1,0)\n",
    "\n",
    "# final outputs\n",
    "rho_f    = pl.fromfile(path_fargo+'gasdens50.dat').reshape(nz,ny,nx).swapaxes(1,0)\n",
    "vphi_f   = pl.fromfile(path_fargo+'gasvx50.dat').reshape(nz,ny,nx).swapaxes(1,0)\n",
    "vrad_f   = pl.fromfile(path_fargo+'gasvy50.dat').reshape(nz,ny,nx).swapaxes(1,0)\n",
    "vtheta_f = pl.fromfile(path_fargo+'gasvz50.dat').reshape(nz,ny,nx).swapaxes(1,0)\n",
    "\n",
    "# correcting for a frame angular velocity\n",
    "omegap = np.loadtxt(path_fargo+'planet0.dat')[:,-1] \n",
    "\n",
    "for it in range(nz):\n",
    "    for ir in range(ny):\n",
    "        vphi0_f[ir,it,:]+=omegap[0]*rad_f[ir]*np.sin(theta_f[it])\n",
    "        vphi_f[ir,it,:]+=omegap[-1]*rad_f[ir]*np.sin(theta_f[it])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Idefix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# path to the data\n",
    "path_idefix = './idefix/'\n",
    "\n",
    "# reading in the data\n",
    "ds0 = readVTK(path_idefix + \"data.0000.vtk\")\n",
    "ds = readVTK(path_idefix + \"data.0500.vtk\")\n",
    "\n",
    "# initial conditions\n",
    "rho0_i = ds0.data[\"RHO\"]\n",
    "vrad0_i = ds0.data[\"VX1\"]\n",
    "vtheta0_i = ds0.data[\"VX2\"]\n",
    "vphi0_i = ds0.data[\"VX3\"]\n",
    "temp0_i = ds0.data[\"TMP\"]\n",
    "\n",
    "# final outputs\n",
    "rho_i = ds.data[\"RHO\"]\n",
    "vrad_i = ds.data[\"VX1\"]\n",
    "vtheta_i = ds.data[\"VX2\"]\n",
    "vphi_i = ds.data[\"VX3\"]\n",
    "temp_i = ds.data[\"TMP\"]\n",
    "\n",
    "# dimensions\n",
    "rad_i = ds.r\n",
    "theta_i = ds.theta\n",
    "phi_i = ds.phi - np.pi\n",
    "nr, ntheta, nphi = (ds.nx, ds.ny, ds.nz)\n",
    "\n",
    "# correcting for a frame angular velocity\n",
    "Omega0 = 1.00049987506246096\n",
    "\n",
    "for it in range(ntheta):\n",
    "    for ir in range(nr):\n",
    "        vphi0_i[ir,it,:]+=Omega0*rad_i[ir]*np.sin(theta_i[it])\n",
    "        vphi_i[ir,it,:]+=Omega0*rad_i[ir]*np.sin(theta_i[it])\n",
    "        \n",
    "# shift arrays along azimuth\n",
    "\n",
    "dn_phi = -int((-1.5638591190e-01+np.pi)/2./np.pi*784)\n",
    "rho_i = np.roll(rho_i, dn_phi, axis=2)\n",
    "vrad_i = np.roll(vrad_i, dn_phi, axis=2)\n",
    "vtheta_i = np.roll(vtheta_i, dn_phi, axis=2)\n",
    "vphi_i = np.roll(vphi_i, dn_phi, axis=2)\n",
    "temp_i = np.roll(temp_i, dn_phi, axis=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Athena++"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# path to the data\n",
    "path_athena = './athena/'\n",
    "\n",
    "# reading in the data\n",
    "data = athena_read.athdf(path_athena + 'athena.out1.00050.athdf')\n",
    "\n",
    "rho_a = data[\"rho\"]  # density\n",
    "vrad_a = data[\"vel1\"] # v_rad\n",
    "vtheta_a = data[\"vel2\"] # v_theta\n",
    "vphi_a = data[\"vel3\"] # v_phi (rotation velocity)\n",
    "\n",
    "# dimensions\n",
    "x1v = data[\"x1v\"]  # radius (mesh center)\n",
    "x2v = data[\"x2v\"]  # theta (mesh center)\n",
    "x3v = data[\"x3v\"]  # phi (mesh center), ranging from -pi to pi\n",
    "x1f = data[\"x1f\"]  # radius (mesh border)\n",
    "x2f = data[\"x2f\"]  # theta (mesh border)\n",
    "x3f = data[\"x3f\"]  # phi (mesh border)\n",
    "n3,n2,n1 = rho_a.shape\n",
    "\n",
    "# correcting for a frame angular velocity\n",
    "Omega0 = 1.00049987506246096\n",
    "\n",
    "for k in range(n2):\n",
    "    for i in range(n1):\n",
    "        vphi_a[:,k,i]+=Omega0*x1v[i]*np.sin(x2v[k])\n",
    "                \n",
    "rho_a = rho_a.T\n",
    "vrad_a = vrad_a.T\n",
    "vtheta_a = vtheta_a.T\n",
    "vphi_a = vphi_a.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pluto"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# path to the data\n",
    "path_pluto = './pluto/'\n",
    "\n",
    "# dimensions\n",
    "rad_p=np.fromfile(path_pluto+'rad')\n",
    "theta_p=np.fromfile(path_pluto+'theta')\n",
    "phi_p=np.fromfile(path_pluto+'phi')\n",
    "n1,n2,n3 = len(rad_p),len(theta_p),len(phi_p)\n",
    "\n",
    "# reading in the data\n",
    "# initial conditions\n",
    "cc=0   \n",
    "filen = '.%(number)04d.dbl' % {\"number\": cc}\n",
    "\n",
    "rho0_p=np.fromfile(path_pluto+'rho'+filen).reshape(n3,n2,n1)\n",
    "vrad0_p=np.fromfile(path_pluto+'vx1'+filen).reshape(n3,n2,n1)\n",
    "vtheta0_p=np.fromfile(path_pluto+'vx2'+filen).reshape(n3,n2,n1)\n",
    "vphi0_p=np.fromfile(path_pluto+'vx3'+filen).reshape(n3,n2,n1)\n",
    "\n",
    "# final outputs\n",
    "cc=500   \n",
    "filen = '.%(number)04d.dbl' % {\"number\": cc}\n",
    "\n",
    "rho_p=np.fromfile(path_pluto+'rho'+filen).reshape(n3,n2,n1)\n",
    "vrad_p=np.fromfile(path_pluto+'vx1'+filen).reshape(n3,n2,n1)\n",
    "vtheta_p=np.fromfile(path_pluto+'vx2'+filen).reshape(n3,n2,n1)\n",
    "vphi_p=np.fromfile(path_pluto+'vx3'+filen).reshape(n3,n2,n1)\n",
    "\n",
    "\n",
    "# correction for a frame angular velocity\n",
    "Omega0 = 1.00049987506246096\n",
    "\n",
    "for k in range(n2):\n",
    "    for i in range(n1):\n",
    "        vphi0_p[:,k,i]+=Omega0*rad_p[i]*np.sin(theta_p[k])\n",
    "        vphi_p[:,k,i]+=Omega0*rad_p[i]*np.sin(theta_p[k])\n",
    "\n",
    "rho0_p = rho0_p.T\n",
    "vrad0_p = vrad0_p.T\n",
    "vtheta0_p = vtheta0_p.T\n",
    "vphi0_p = vphi0_p.T\n",
    "\n",
    "rho_p = rho_p.T\n",
    "vrad_p = vrad_p.T\n",
    "vtheta_p = vtheta_p.T\n",
    "vphi_p = vphi_p.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Phantom"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# path to the data\n",
    "path_phantom = './phantom/'\n",
    "\n",
    "# fargo3d mesh grid\n",
    "qq = np.meshgrid(rad_f,theta_f,phi_f,indexing='ij')\n",
    "rad = qq[0]\n",
    "theta = qq[1]\n",
    "phi = qq[2]\n",
    "\n",
    "# reading in phantom result\n",
    "rho_ph = pl.fromfile(path_phantom+'phantom_density_interpolated_nearest.dat')\n",
    "rho_ph = rho_ph.reshape(nr,ntheta,nphi)\n",
    "\n",
    "vx_ph = pl.fromfile(path_phantom+'phantom_vx_interpolated_nearest.dat')\n",
    "vx_ph = vx_ph.reshape(nr,ntheta,nphi)\n",
    "\n",
    "vy_ph = pl.fromfile(path_phantom+'phantom_vy_interpolated_nearest.dat')\n",
    "vy_ph = vy_ph.reshape(nr,ntheta,nphi)\n",
    "\n",
    "vz_ph = pl.fromfile(path_phantom+'phantom_vz_interpolated_nearest.dat')\n",
    "vz_ph = vz_ph.reshape(nr,ntheta,nphi)\n",
    "\n",
    "# convert cartesian velocities to spherical velocities\n",
    "vrad_ph = vx_ph*np.sin(phi) + vy_ph*np.cos(phi) + vz_ph*np.cos(theta)\n",
    "vphi_ph = -vx_ph*np.cos(phi) + vy_ph*np.sin(phi)\n",
    "vtheta_ph = -vz_ph*np.sin(theta)\n",
    "\n",
    "# shift arrays along azimuth\n",
    "rho_ph = np.roll(rho_ph, 592, axis=2)[:,:,::-1]\n",
    "vrad_ph = np.roll(vrad_ph, 592, axis=2)[:,:,::-1]\n",
    "vphi_ph = np.roll(vphi_ph, 592, axis=2)[:,:,::-1]\n",
    "vtheta_ph = np.roll(vtheta_ph, 592, axis=2)[:,:,::-1]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
